package com.easyui.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.easyui.bean.Student;
import com.easyui.common.SelAttach;
import com.easyui.common.SelNote;
import com.easyui.common.SelStudent;

public class DelNoteServlet extends HttpServlet {
       
    public DelNoteServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
		

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/json;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		JSONObject jsonAll = null;
		boolean success = false;
		String msg = "";
		
		int nno = Integer.parseInt(request.getParameter("nno"));
		int pid = Integer.parseInt(request.getParameter("pid"));
		
		HttpSession session = request.getSession(); //获的session
		String role = (String)session.getAttribute("role");
		String userID = (String)session.getAttribute("userID");
		
		if(role != null && !role.trim().equals("")) {
			if(userID != null && !userID.trim().equals("")) {//session未过期
				if(nno != -1) { 
					if(new SelNote().canDelNote(nno)) {//是否可以删除，有回帖则不能删除
						if(new SelNote().deleteNote(nno, pid)) {//删除该贴
							success = new SelNote().updatePNoteLeaf(pid);//是否更新父贴为叶子节点
						} else {
							success = false;
							msg = "删除该帖失败";
						}
					} else {
						success = false;
						msg = "该贴已有回帖，不能删除<br/>请刷新页面.";
					}
					
				} else {
					success = false;
					msg = "删除该帖失败.";
				}
			} else {
				success = false;
				msg = "登陆已过期，请从新登陆.";
			}
		} else {
			success = false;
			msg = "登陆已过期，请从新登陆.";
		}
		
		if(success) {
			msg = "成功删除回帖";
		} else {
			msg = "删除回帖失败";
		}
		
		jsonAll= new JSONObject();
		jsonAll.put("success", success);
		jsonAll.put("msg", msg);
		
		out.print(jsonAll);
		out.flush();
		out.close();
	}

}
